package demo.practice.leetcode_base;

import java.util.Stack;

public class Ms0206 {

    public static void main(String[] args) {
        Ms0206 ms0206= new Ms0206();
        ListNode h= new ListNode(-129);
        h.next= new ListNode(-129);

        ms0206.isPalindrome(h);
    }
    public boolean isPalindrome(ListNode head) {

        //将链表的值先放到list中
        /*List<Integer> l = new ArrayList<>();
        ListNode current = head;
        while (current != null) {
            l.add(current.val);
            current = current.next;
        }

        int size = l.size();
        for(int i=0;i<size/2;i++){//5   //i<2   0,1,
            if(!l.get(i).equals(l.get(l.size()-1-i)))
                return false;
        }
        return true;*/

        int size=0;
        ListNode current = head;
        while (current != null) {
            size++;
            current = current.next;
        }

        current = head;
        int time=0;
        Stack<Integer> stack= new Stack<>();
        while (time<size/2) {
            time++;
            stack.add(current.val);
            current = current.next;
        }

        if(size%2==1) {
            current=current.next;
        }

        while (current != null) {
            if(current.val!=stack.pop()) {
                return false;
            }
            current = current.next;
        }
        return true;
    }
}
